package com.sinosoft.jwjw.base;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinosoft.jwjw.utils.ConvertUtil;
import com.sinosoft.jwjw.utils.ResultDetail;
import com.sinosoft.jwjw.utils.ResultPage;
import net.sf.json.JSONArray;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 父类的抽象接口类；
 *
 * @param <T>
 * @param <PK>
 */
public abstract class BaseCrudServiceImp<T, PK extends Serializable> implements BaseCrudService<T, PK> {

    @Override
    public abstract BaseCrudMapper<T, PK> getMapper();


    /**
     * 物理删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String id) {
        return getMapper().deleteByPrimaryKey(id);
    }

    /**
     * 新增对象
     *
     * @param t
     * @return
     */
    @Override
    public int insert(T t) {
        return getMapper().insert(t);
    }

    /**
     * 更新对象所有属性
     *
     * @param t
     * @return
     */
    @Override
    public int updateByPrimaryKey(T t) {
        return getMapper().updateByPrimaryKey(t);
    }

    /**
     * 根据id获取对象详情
     *
     * @param id
     * @return
     */
    @Override
    public T selectByPrimaryKey(String id) {
        return getMapper().selectByPrimaryKey(id);
    }

    /**
     * 查询所有对象列表
     *
     * @return
     */
    @Override
    public List<T> selectAll() {
        return getMapper().selectAll();
    }


    /**
     * 分页查询列表
     *
     * @return
     * @params 必传参数：
     * @pageindex int
     * @pagesize int
     * 可选参数：
     */
    @Override
    public ResultPage getListPage(Map<String, Object> params) {

        ResultPage resultPage = new ResultPage();

        int pageIndex = ConvertUtil.toInt(params.get("pageIndex"), 1);
        int pageSize = ConvertUtil.toInt(params.get("pageSize"), 20);

        try {
            Page page = PageHelper.startPage(pageIndex, pageSize, true);
            JSONArray list = getMapper().getListPage(params);

            resultPage.setCode(0);
            resultPage.setMsg("成功");
            resultPage.setPageIndex(pageIndex);
            resultPage.setPageSize(pageSize);
            resultPage.setAllcount(page.getTotal());
            resultPage.setResult(list);

        } catch (Exception e) {

            resultPage.setCode(500);
            resultPage.setMsg("呀，网络不太好~");
            resultPage.setPageIndex(pageIndex);
            resultPage.setPageSize(pageSize);
            resultPage.setAllcount(0L);
            resultPage.setResult(new JSONArray());

        } finally {
            return resultPage;
        }
    }


    /**
     * 逻辑修改用户
     *
     * @return
     * @params 必传参数 @id String
     * 可选参数：@chineseName String
     */
    @Override
    public ResultDetail updateById(Map<String, Object> params) {

        ResultDetail result = new ResultDetail();

        int rows = getMapper().updateById(params);
        if (rows > 0) {
            result.setCode(0);
            result.setMsg("保存成功！");
            result.setInnermsg("");
            result.setResult(null);
        } else {
            result.setCode(500);
            result.setMsg("保存失败！");
            result.setInnermsg("");
            result.setResult(null);
        }
        return result;
    }

    /**
     * 逻辑删除对象(批量)
     *
     * @param
     * @return
     */
    @Override
    public ResultDetail deleteByIds(JSONArray ids) {

        ResultDetail result = new ResultDetail();

        for (int i = 0; i < ids.size(); i++) {
            String id = ConvertUtil.toString(ids.get(i));
            if (!"".equals(id)) {
                int rows = getMapper().deleteById(id);
//                if (rows > 0) {
//                    result.setCode(0);
//                    result.setMsg("删除成功！");
//                    result.setInnermsg("");
//                    result.setResult(null);
//                } else {
//                    result.setCode(500);
//                    result.setMsg("删除失败！");
//                    result.setInnermsg("");
//                    result.setResult(null);
//                }
            }
        }

        result.setCode(0);
        result.setMsg("批量删除成功！");
        result.setInnermsg("");
        result.setResult(null);

        return result;
    }


    /**
     * 逻辑删除对象
     *
     * @param
     * @return
     */
    @Override
    public ResultDetail deleteById(String id) {

        ResultDetail result = new ResultDetail();

        int rows = getMapper().deleteById(id);
        if (rows > 0) {
            result.setCode(0);
            result.setMsg("删除成功！");
            result.setInnermsg("");
            result.setResult(null);
        } else {
            result.setCode(500);
            result.setMsg("删除失败！");
            result.setInnermsg("");
            result.setResult(null);
        }
        return result;
    }

    /**
     * 插入对象 返回ResultDetail
     *
     * @param t
     * @return
     */
    @Override
    public ResultDetail inserts(T t) {
        ResultDetail result = new ResultDetail();

        int rows = getMapper().insert(t);
        if (rows > 0) {
            result.setCode(0);
            result.setMsg("保存成功！");
            result.setInnermsg("");
            result.setResult(null);
        } else {
            result.setCode(500);
            result.setMsg("保存失败！");
            result.setInnermsg("");
            result.setResult(null);
        }
        return result;
    }

}
